import request from '@/utils/request'

// 查询国际化词条列表
export function listSysLanguage(query) {
  return request({
    url: '/language/sysLanguage/list',
    method: 'get',
    params: query
  })
}

// 获取国际化词条
export function getLanguage(language, tenantCode) {
  return new Promise((resolve, reject) => {
    // 如果language 为空 则优先从localStorage中获取 如果获取不到在调用后台获取
    var languageJson
    if (language === undefined) {
      var localStorage = window.localStorage.getItem('language')
      if (localStorage != null && language === undefined) {
        language = localStorage
      }
      if (tenantCode === undefined || tenantCode === '') {
        if (language !== undefined) {
          var storageLanguageJson = window.localStorage.getItem('language_' + language)
          if (storageLanguageJson !== undefined && storageLanguageJson !== 'undefined') {
            try {
              languageJson = JSON.parse(storageLanguageJson)
            } catch (err) {
              // console.log(err)
            }
          }
        }
        if (languageJson !== undefined) {
          var languageData = {}
          languageData.language = language
          languageData.languageData = languageJson
          resolve(languageData)
        }
      }
    }
    if (languageJson === undefined) {
      return request({
        url: '/language/sysLanguage/getLanguage/' + language + '/' + tenantCode,
        method: 'get'
      }).then(response => {
        var languageData = {}
        languageData.language = response.datas.language
        languageData.languageData = response.data
        resolve(languageData)
      }).catch(err => {
        reject(err)
      })
    }
  })
}

// 修改国际化词条
export function updateLanguage(language) {
  return request({
    url: '/language/sysLanguage/updateLanguage/' + language,
    method: 'get'
  })
}

// 查询国际化词条详细
export function getSysLanguage(id) {
  return request({
    url: '/language/sysLanguage/' + id,
    method: 'get'
  })
}

// 保存国际化词条
export function saveSysLanguage(data) {
  return request({
    url: '/language/sysLanguage/save',
    method: 'post',
    data: data
  })
}

// 删除国际化词条
export function delSysLanguage(id) {
  return request({
    url: '/language/sysLanguage/' + id,
    method: 'delete'
  })
}

// 导出国际化词条
export function exportSysLanguage(query) {
  return request({
    url: '/language/sysLanguage/export',
    method: 'get',
    params: query
  })
}

// 根据编码获取国际化词条信息
export function getByCode(code) {
  return request({
    url: '/language/sysLanguage/getByCode',
    method: 'post',
    data: code
  })
}
